﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Engine;

/* Copyright (c) 2014
 * author: Ricky Kratochwil
 * @ date: 2014-06-25
 * @ summary: Used to set all relevant game options (e.g. player selection)
*/

namespace GameOfCoins
{
	public partial class GameSetup : Form
	{

		private bool	m_bP1OK;
		private int		m_iP1ID;
		private bool	m_bP2OK;
		private int		m_iP2ID;
		GameMainFrame	m_oMainFrame;		//Use for initialization calls

		public GameSetup(GameMainFrame oMainFrame)
		{
			InitializeComponent();

			m_bP1OK = false;
			m_iP1ID	= -1;
			m_bP2OK = false;
			m_iP2ID = -1;

			m_oMainFrame = oMainFrame;

			LoadPlayerImages();
			LoadAvailableWorlds();

			//Players must select templates one after another
			btnConfirmP2.Text		= "...warten auf P1";
			btnConfirmP2.Enabled	= false;
		}

//////////////////////////////////////////////////////////////////////////////////////////////////////
////////// UTILITIES

		//Load player graphics and set to picture boxes
		//@ KR / 2014.06.25
		private void LoadPlayerImages()
		{
			pbTemplateP1.Image = Engine.Properties.Resources.P1_small;
			pbTemplateP2.Image = Engine.Properties.Resources.P2_small;
			pbTemplateP3.Image = Engine.Properties.Resources.P3_small;
			pbTemplateP4.Image = Engine.Properties.Resources.P4_small;
		}

		//Load saved worlds and display for selection
		//@ KR / 2014.06.25
		private void LoadAvailableWorlds()
		{
			string[] strWorldFiles = Directory.GetFiles(Directory.GetCurrentDirectory() + "\\Worlds", "*.gcw");
			int iStart, iLenght;

			foreach(string strFile in strWorldFiles)
			{
				iStart	= strFile.LastIndexOf('\\')+1;
				iLenght	= strFile.LastIndexOf('.') - iStart;

				lbWorldsAvail.Items.Add(strFile.Substring(iStart, iLenght));
			}
		}

//////////////////////////////////////////////////////////////////////////////////////////////////////
////////// PLAYER SELECTION

		//Player has selected player template
		//@ KR / 2014.06.25
		private void pbTemplateP1_Click(object sender, EventArgs e)
		{
			//If player 1 is false, selection is not done yet
			if(!m_bP1OK)
			{
				pbSelectP1.Image = new Bitmap(Engine.Properties.Resources.P1_small, new Size(48,48));
				m_iP1ID			 = 1;
			}
			else	//Player 2
			{
				pbSelectP2.Image = new Bitmap(Engine.Properties.Resources.P1_small, new Size(48,48));;
				m_iP2ID			 = 1;
			}
		}
		private void pbTemplateP1_DoubleClick(object sender, EventArgs e)
		{
			if(!m_bP1OK)
				btnConfirmP1_Click(this, e);
			else
				btnConfirmP2_Click(this, e);
		}

		//Player has selected player template
		//@ KR / 2014.06.25
		private void pbTemplateP2_Click(object sender, EventArgs e)
		{
			//If player 1 is false, selection is not done yet
			if(!m_bP1OK)
			{
				pbSelectP1.Image = new Bitmap(Engine.Properties.Resources.P2_small, new Size(48,48));
				m_iP1ID			 = 2;
			}
			else	//Player 2
			{
				pbSelectP2.Image = new Bitmap(Engine.Properties.Resources.P2_small, new Size(48,48));;
				m_iP2ID			 = 2;
			}
		}
		private void pbTemplateP2_DoubleClick(object sender, EventArgs e)
		{
			if(!m_bP1OK)
				btnConfirmP1_Click(this, e);
			else
				btnConfirmP2_Click(this, e);
		}

		//Player has selected player template
		//@ KR / 2014.06.25
		private void pbTemplateP3_Click(object sender, EventArgs e)
		{
			//If player 1 is false, selection is not done yet
			if(!m_bP1OK)
			{
				pbSelectP1.Image = new Bitmap(Engine.Properties.Resources.P3_small, new Size(48,48));
				m_iP1ID			 = 3;
			}
			else	//Player 2
			{
				pbSelectP2.Image = new Bitmap(Engine.Properties.Resources.P3_small, new Size(48,48));;
				m_iP2ID			 = 3;
			}
		}
		private void pbTemplateP3_DoubleClick(object sender, EventArgs e)
		{
			if(!m_bP1OK)
				btnConfirmP1_Click(this, e);
			else
				btnConfirmP2_Click(this, e);
		}

		//Player has selected player template
		//@ KR / 2014.06.25
		private void pbTemplateP4_Click(object sender, EventArgs e)
		{
			//If player 1 is false, selection is not done yet
			if(!m_bP1OK)
			{
				pbSelectP1.Image = new Bitmap(Engine.Properties.Resources.P4_small, new Size(48,48));
				m_iP1ID			 = 4;
			}
			else	//Player 2
			{
				pbSelectP2.Image = new Bitmap(Engine.Properties.Resources.P4_small, new Size(48,48));;
				m_iP2ID			 = 4;
			}
		}
		private void pbTemplateP4_DoubleClick(object sender, EventArgs e)
		{
			if(!m_bP1OK)
				btnConfirmP1_Click(this, e);
			else
				btnConfirmP2_Click(this, e);
		}


		//Player1 confirm selection
		//@ KR / 2014.06.25
		private void btnConfirmP1_Click(object sender, EventArgs e)
		{
			if(m_iP1ID != -1)
			{
				//Enable p2 button
				btnConfirmP2.Text		= "OK";
				btnConfirmP2.Enabled	= true;

				//Disable p1 button
				btnConfirmP1.Enabled	= false;

				//Remove 3D style from picturebox
				pbSelectP1.BorderStyle = BorderStyle.FixedSingle;

				//Confirm p1 selected
				m_bP1OK = true;
			}
			else
				MessageBox.Show("Sie haben noch keinen Spieler gewählt", "Nicht möglich!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
		}

		//Player2 confirm selection
		//@ KR / 2014.06.25
		private void btnConfirmP2_Click(object sender, EventArgs e)
		{
			if(m_iP2ID != -1)
			{
				//Disable p2 button
				btnConfirmP2.Enabled	= false;

				//Remove 3D style from picturebox
				pbSelectP2.BorderStyle = BorderStyle.FixedSingle;

				//Confirm p1 selected
				m_bP2OK = true;
			}
			else
				MessageBox.Show("Sie haben noch keinen Spieler gewählt", "Nicht möglich!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
		}

//////////////////////////////////////////////////////////////////////////////////////////////////////
////////// WORLD SELECTION

		//Move selected world to active list
		//@ KR / 2014.06.25
		private void btnAddWorld_Click(object sender, EventArgs e)
		{
			object oSelected = lbWorldsAvail.SelectedItem;

			if(oSelected != null)
			{
				lbWorldsUsed.Items.Add(oSelected);
				lbWorldsAvail.Items.Remove(oSelected);

			}
		}

		//Remove selected world from active list
		//@ KR / 2014.06.25
		private void btnRemoveWorld_Click(object sender, EventArgs e)
		{
			object oSelected = lbWorldsUsed.SelectedItem;

			if(oSelected != null)
			{
				lbWorldsAvail.Items.Add(oSelected);
				lbWorldsUsed.Items.Remove(oSelected);

			}
		}

		private void lbWorldsAvail_MouseDoubleClick(object sender, MouseEventArgs e)
		{
			btnAddWorld_Click(sender, e);
		}

		private void lbWorldsUsed_MouseDoubleClick(object sender, MouseEventArgs e)
		{
			btnRemoveWorld_Click(sender, e);
		}

//////////////////////////////////////////////////////////////////////////////////////////////////////
////////// COMMOM CTRLS

		//Start game now
		//@ KR / 2014.06.25
		private void btnStart_Click(object sender, EventArgs e)
		{
			//Check if all selections are done
			if(!m_bP1OK || !m_bP2OK || lbWorldsUsed.Items.Count < 1)
			{
				MessageBox.Show("Es wurden noch nicht alle Einstellungen getroffen", "Nicht möglich!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
				DialogResult = DialogResult.None;
			}
			else
			{
				string[] strWorldFiles;

				//Extract worlds
				strWorldFiles = new string[lbWorldsUsed.Items.Count];
				for(int i = 0; i < lbWorldsUsed.Items.Count; i++)
					strWorldFiles[i] = Convert.ToString(lbWorldsUsed.Items[i]);
				

				m_oMainFrame.Init(strWorldFiles, m_iP1ID, m_iP2ID);
			}
		}
	}
}
